package com.premedsidekick;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class Temp_HorizontalScrollView extends Activity {
	int flag = 0;
	int rememberer_core = 0;
	int rememberer_additional = 4;
	boolean additional = false;
	boolean quarter_system;
	Button button;
	DefaultCoursesDatabaseAdapter dbHelper;
	ArrayAdapter<CharSequence> arrayadapter;
	Cursor cursor;
	LinearLayout l_additional;
	LinearLayout l_core;
	HorizontalScrollView horizontalscrollview;
	CharSequence sequence;
	SharedPreferences preferences;
	Editor editor;
	
	Integer[] buttons = {
			R.id.course_requirements_button_1,
			R.id.course_requirements_button_2,
			R.id.course_requirements_button_3,
			R.id.course_requirements_button_4,
			R.id.course_requirements_button_5,
			R.id.course_requirements_button_6,
			R.id.course_requirements_button_7,
			R.id.course_requirements_button_8,
			R.id.course_requirements_button_9,
	};
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.course_requirements_horizontal_scrollbar);
		dbHelper = new DefaultCoursesDatabaseAdapter(this);
		dbHelper.open();
		cursor = dbHelper.getAll();
		//System.out.println("Database has #: " + cursor.getCount());
		if (cursor.getCount() == 0) {
			//System.out.println("Recreating entries");
			dbHelper.create();
		}
		
		horizontalscrollview = (HorizontalScrollView)findViewById(R.id.course_requirements_horizontal_scroll_view);
		//System.out.println("hsv: " + horizontalscrollview);
		
		quarter_system = false;
		ListView lv = (ListView)findViewById(R.id.course_requirements_list_view);
		final CourseRequirementsBaseAdapter adapter = new CourseRequirementsBaseAdapter(this);
		lv.setAdapter(adapter);
		lv.setDivider(null);
		lv.setBackgroundColor(Color.BLACK);
		
		LayoutInflater inflater = LayoutInflater.from(this);
		l_additional = (LinearLayout)inflater.inflate(R.layout.hscrollview_additional, null);
		//System.out.println("hsvladditional: " + l_additional);
		l_core = (LinearLayout)inflater.inflate(R.layout.hscrollview_core, null);
		//System.out.println("hsvlcore: " + l_core);
		
		for (int i = 0; i < 4; i++) {
			final int j = i;
			button = (Button)l_core.findViewById(buttons[i]);
			//System.out.println(i + " " + button);
			button.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					flag = j;
					adapter.notifyDataSetChanged();
				}
			});
		}
		for (int i = 4; i < 9; i++) {
			final int j = i;
			button = (Button)l_additional.findViewById(buttons[i]);
			button.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					flag = j;
					adapter.notifyDataSetChanged();
				}
			});
		}
		
		horizontalscrollview.addView(l_core);
		
		CheckBox checkbox = (CheckBox)findViewById(R.id.course_requirements_quarter_check_box);
		checkbox.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				if (quarter_system == false) {
					quarter_system = true;
					adapter.notifyDataSetChanged();
				}
				else { 
					quarter_system = false;
					adapter.notifyDataSetChanged();
				}
			}
			
		});
		
		CheckBox additionalbox = (CheckBox)findViewById(R.id.course_requirements_additional_check_box);
		additionalbox.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				if (additional == false) {
					additional = true;
					rememberer_core = flag;
					flag = rememberer_additional;
					swap();
					adapter.notifyDataSetChanged();
				}
				else { 
					additional = false;
					rememberer_additional = flag;
					flag = rememberer_core;
					swap();
					adapter.notifyDataSetChanged();
				}
			}
			
		});
		/*
		preferences = getSharedPreferences("user_settings", 0);
		editor = preferences.edit();
		int usages = preferences.getInt("usages", 0);
		if (usages < 2) {
			final int state = usages + 1;
			Dialog dialog = new Dialog(this);
			dialog.setCancelable(true);
			dialog.setTitle("Basic Instructions");
			dialog.setContentView(R.layout.maindialog);
			TextView textview = (TextView)dialog.findViewById(R.id.TextView01);
			String message = getResources().getString(R.string.instructions);
			CharSequence sequence = Html.fromHtml(message);
			textview.setText(sequence);
			dialog.setOnCancelListener(new OnCancelListener() {

				@Override
				public void onCancel(DialogInterface dialog) {
					editor.putInt("usages", state);
					editor.commit();
				}
				
			});
			dialog.show();
		}
		*/
	}
	
	public void swap() {
		if (additional == false) {
			horizontalscrollview.removeAllViews();
			horizontalscrollview.addView(l_core);
		}
		else {
			horizontalscrollview.removeAllViews();
			horizontalscrollview.addView(l_additional);
		}
	}
	public void onDestroy() {
		super.onDestroy();
		cursor.close();
		dbHelper.close();
		
	}
	public class CourseRequirementsBaseAdapter extends BaseAdapter {
		LayoutInflater inflater;
		//Viewholder viewholder;
		Context context;
		String[] semesters;
		String[] quarters;
		ArrayAdapter<CharSequence> arrayadapter;
		
		CourseRequirementsBaseAdapter(Context context) {
			super();
			this.context = context;
			inflater = LayoutInflater.from(context);
			quarters = getResources().getStringArray(R.array.course_quarters);
			semesters = getResources().getStringArray(R.array.course_semesters);
			
		}
		@Override
		public int getCount() {
			if (quarter_system == false)
				return 2;
			else
				return 3;
		}
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}
		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			//if (additional == false) {
				if (quarter_system == false) {
					//System.out.println("List position: " + position);
					if (position < 2) {
						if (convertView == null || convertView.getTag(position) == null) {
							
							final int index = position;
							//viewholder = new Viewholder();
							convertView = inflater.inflate(R.layout.course_listing, null);
							final TextView textview = (TextView)convertView.findViewById(R.id.course_name);
							textview.setText(semesters[flag * 2 + position % 2]);
							final Spinner spinner = (Spinner)convertView.findViewById(R.id.course_grade);
							arrayadapter = ArrayAdapter.createFromResource(context, R.array.possible_grades, android.R.layout.simple_spinner_item);
							spinner.setAdapter(arrayadapter);
							cursor.deactivate();
							cursor = dbHelper.getSemester((long)flag);
							//System.out.println("count: " + cursor.getCount());
							//System.out.println("pos: " + index);
							cursor.moveToFirst();
							String grade = cursor.getString(position);
							int value = Integer.parseInt(grade);
							spinner.setSelection(value);
							spinner.setOnItemSelectedListener(new OnItemSelectedListener () {
	
								@Override
								public void onItemSelected(AdapterView<?> arg0,
										View arg1, int arg2, long arg3) {
									Integer convert = new Integer(spinner.getSelectedItemPosition());
									boolean temp = dbHelper.updateSemester(flag, index, convert.toString());
									//System.out.println("Successful: " + temp);
								}
	
								@Override
								public void onNothingSelected(AdapterView<?> arg0) {
									// TODO Auto-generated method stub
									
								}
								
							});
						}
					else {
						/*
							viewholder = (Viewholder)convertView.getTag(position);
							viewholder.textview.setText(semesters[flag * 2 + position % 2]);
							System.out.println("Does this run on switch");
							cursor = dbHelper.getSemester(flag);
							String grade = cursor.getString(position);
							int value = Integer.parseInt(grade);
							viewholder.spinner.setSelection(value);
							*/
						}
					}
				
				}
				// quarter handler
				else {
					
					if (position < 3) {
						if (convertView == null || convertView.getTag(position) == null) {
							final int index = position;
							
							convertView = inflater.inflate(R.layout.course_listing, null);
							final TextView textview = (TextView)convertView.findViewById(R.id.course_name);
							//textview.setText("1st Semester:\nBiochemistry w/Lab");
							//textview.clearComposingText();
							textview.setText(quarters[flag * 3 + position % 3]);
							final Spinner spinner = (Spinner)convertView.findViewById(R.id.course_grade);
							arrayadapter = ArrayAdapter.createFromResource(context, R.array.possible_grades, android.R.layout.simple_spinner_item);
							spinner.setAdapter(arrayadapter);
							cursor.deactivate();
							cursor = dbHelper.getQuarter((long)flag);
							//System.out.println("count: " + cursor.getCount());
							//System.out.println("pos: " + index);
							cursor.moveToFirst();
							String grade = cursor.getString(position);
							int value = Integer.parseInt(grade);
							spinner.setSelection(value);
							spinner.setOnItemSelectedListener(new OnItemSelectedListener () {
	
								@Override
								public void onItemSelected(AdapterView<?> arg0,
										View arg1, int arg2, long arg3) {
									Integer convert = new Integer(spinner.getSelectedItemPosition());
									boolean temp = dbHelper.updateQuarter(flag, index, convert.toString());
									//System.out.println("Successful: " + temp);
								}
	
								@Override
								public void onNothingSelected(AdapterView<?> arg0) {
									// TODO Auto-generated method stub
									
								}
								
							});
						}
						else {
							/*
							viewholder = (Viewholder)convertView.getTag(position);
							viewholder.textview.setText(quarters[flag * 3 + position % 3]);
							cursor = dbHelper.getQuarter(flag);
							String grade = cursor.getString(position);
							int value = Integer.parseInt(grade);
							viewholder.spinner.setSelection(value);
							*/
						}
					}
				}
			//}
		
		return convertView;
		}
	}

	/*
	 * 0: Biology
	 * 1: General Chemistry
	 * 2: Organic Chemistry
	 * 3: Physics
	 */
	/*
	public class CourseRequirementsDatabaseAdapter {
		int DATABASE_VERSION = 1;
		String DATABASE_NAME = "course_requirements_table";
		String KEY_ROWID = "_id";
		String KEY_QUARTER_FLAG = "quarterflag";
		String KEY_FIRSTSEMESTER = "firstsemester";
		String KEY_SECONDSEMESTER = "secondsesmeter";
		String KEY_FIRSTQUARTER = "firstquarter";
		String KEY_SECONDQUARTER = "secondquarter";
		String KEY_THIRDQUARTER = "thirdquarter";
		String KEY_COMPLETE = "complete";
		Context context;
		SQLiteDatabase database;
		CourseRequirementsDatabaseHelper dbHelper;
		ContentValues values;
		String[] semester_list = {KEY_FIRSTSEMESTER, KEY_SECONDSEMESTER};
		String[] quarter_list = {KEY_FIRSTQUARTER, KEY_SECONDQUARTER, KEY_THIRDQUARTER};
		
		CourseRequirementsDatabaseAdapter(Context context) {
			this.context = context;
		}
		
		public CourseRequirementsDatabaseAdapter open() throws SQLException {
			dbHelper = new CourseRequirementsDatabaseHelper(context);
			database = dbHelper.getWritableDatabase();
			return this;
		}
		public void close() {
			dbHelper.close();
		}
		public void create() {
			for (int i = 0; i < 9; i++) {
				values = createInitialValues(i);
				database.insert(DATABASE_NAME, null, values);
			} 
		}
		public boolean updateQuarterFlag(long rowId, String flag) {
			values = new ContentValues();
			values.put(KEY_QUARTER_FLAG, flag);
			return database.update(DATABASE_NAME, values, KEY_ROWID + "=" + rowId, null) > 0;
		}
		public boolean updateSemester(long rowId, int position, String grade) {
			values = new ContentValues();
			System.out.println("Row id: " + rowId);
			System.out.println("Semester List: " + semester_list[position]);
			System.out.println("Grade: " + grade);
			values.put(semester_list[position], grade);
			return database.update(DATABASE_NAME, values, KEY_ROWID + "=" + rowId, null) > 0;
		}
		public boolean updateQuarter(long rowId, int position, String grade) {
			values = new ContentValues();
			values.put(quarter_list[position], grade);
			return database.update(DATABASE_NAME, values, KEY_ROWID + "=" + rowId, null) > 0;
		}
		
		public boolean updateSemester(long rowId, String first, String second) {
			values = createSemesterValues(first, second);
			return database.update(DATABASE_NAME, values, KEY_ROWID + "=" + rowId, null) > 0;
		}
		public boolean updateQuarter(long rowId, String first, String second, String third) {
			values = createQuarterValues(first, second, third);
			return database.update(DATABASE_NAME, values, KEY_ROWID + "=" + rowId, null) > 0;
		}
		
		public boolean delete() {
			return database.delete(DATABASE_NAME, null, null) > 0;
		}

		public Cursor getRow(long rowId) {
			return database.query(DATABASE_NAME, new String[] {KEY_ROWID, KEY_QUARTER_FLAG,
					KEY_FIRSTSEMESTER, KEY_SECONDSEMESTER, KEY_FIRSTQUARTER, KEY_SECONDQUARTER,
					KEY_THIRDQUARTER, KEY_COMPLETE}, KEY_ROWID + "=" + rowId, null, null, null, null);
		}
		public Cursor getSemester(long rowId) {
			return database.query(DATABASE_NAME, new String[] {KEY_FIRSTSEMESTER,
					KEY_SECONDSEMESTER}, KEY_ROWID + "=" + rowId, null, null, null, null);
		}
		public Cursor getAllSemester() {
			return database.query(DATABASE_NAME, new String[] {KEY_FIRSTSEMESTER,
					KEY_SECONDSEMESTER}, null, null, null, null, null);
		}
		public Cursor getQuarter(long rowId) {
			return database.query(DATABASE_NAME, new String[] {KEY_FIRSTQUARTER,
					KEY_SECONDQUARTER, KEY_THIRDQUARTER}, KEY_ROWID + "=" + rowId, null, null, null, null);
		}
		public Cursor getAllQuarter() {
			return database.query(DATABASE_NAME, new String[] {KEY_FIRSTQUARTER,
					KEY_SECONDQUARTER, KEY_THIRDQUARTER}, null, null, null, null, null);
		}
		public Cursor getAll() {
			return database.query(DATABASE_NAME, new String[] {KEY_ROWID, KEY_QUARTER_FLAG,
					KEY_FIRSTSEMESTER, KEY_SECONDSEMESTER, KEY_FIRSTQUARTER, KEY_SECONDQUARTER,
					KEY_THIRDQUARTER, KEY_COMPLETE}, null, null, null, null, null);
		}
		public class CourseRequirementsDatabaseHelper extends SQLiteOpenHelper {
			CourseRequirementsDatabaseHelper(Context context) {
				super(context, DATABASE_NAME, null, DATABASE_VERSION);
			}

			@Override
			public void onCreate(SQLiteDatabase db) {
				db.execSQL("CREATE TABLE " + DATABASE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
						KEY_QUARTER_FLAG + " TEXT, " + KEY_FIRSTSEMESTER + " TEXT, " + KEY_SECONDSEMESTER + " TEXT, " +
						KEY_FIRSTQUARTER + " TEXT, " + KEY_SECONDQUARTER + " TEXT, " + KEY_THIRDQUARTER + " TEXT, " +
						KEY_COMPLETE + " TEXT);");	
			}

			@Override
			public void onUpgrade(SQLiteDatabase db, int oldVersion,
					int newVersion) {				
			}
			
		}
		public ContentValues createInitialValues(int i) {
			ContentValues values = new ContentValues();
			values.put(KEY_ROWID, i);
			values.put(KEY_QUARTER_FLAG, "false");
			values.put(KEY_FIRSTSEMESTER, "0");
			values.put(KEY_SECONDSEMESTER, "0");
			values.put(KEY_FIRSTQUARTER, "0");
			values.put(KEY_SECONDQUARTER, "0");
			values.put(KEY_THIRDQUARTER, "0");
			values.put(KEY_COMPLETE, "false");
			return values;
		}
		public ContentValues createQuarterValues(String first, String second, String third) {
			ContentValues values = new ContentValues();
			values.put(KEY_FIRSTQUARTER, first);
			values.put(KEY_SECONDQUARTER, second);
			values.put(KEY_THIRDQUARTER, third);
			return values;
		}
		public ContentValues createSemesterValues(String first, String second) {
			ContentValues values = new ContentValues();
			values.put(KEY_FIRSTSEMESTER, first);
			values.put(KEY_SECONDSEMESTER, second);
			return values;
		}
	}
	*/
}
